package com.zjp.basetypes;

import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Component
public class RedisHyperLogLog {
    public static final String HLL = "hll:";
    @Autowired
    JedisPool jedisPool;

    public void pfadd() {

        try (Jedis resource = jedisPool.getResource()) {
            Long pfadd = resource.pfadd(HLL + DateUtil.today(), "1", "2");
            // 如果添加成功返回 1
            System.out.println("pfadd " + pfadd);
        }
    }

    public void pfcount() {
        // pfcount 用于计算一个或多个 HyperLogLog 的独立总数
        try (Jedis resource = jedisPool.getResource()) {
            long pfcount = resource.pfcount(HLL + DateUtil.today());

            System.out.println("pfcount " + pfcount);

        }

    }

    public void pfmerge() {
        //pfmerge 可以求出多个 HyperLogLog 的并集并赋值给 destkey
        String s = HLL + "destkey";
        try (Jedis resource = jedisPool.getResource()) {
            String pfmerge = resource.pfmerge(s, HLL + DateUtil.today());

            System.out.println("pfmerge " + pfmerge);
            long pfcount = resource.pfcount(s);

            System.out.println("pfcount " + pfcount);
        }

    }

}
